rm(list=ls())
library(data.table)
library(xtable)
library(tidyr)
library(dplyr)
library(tibble)
library(readr)

files = paste0('results/',
              c('current-results-2012-2016.Rdata',
                'current-results-2016-2020.Rdata',
                'current-results-2012-2016-pretrend.Rdata',
                'current-results-2016-2020-pretrend.Rdata',
                'current-results-2012-2016-full-knn.Rdata',
                'current-results-2016-2020-full-knn.Rdata',
                'current-results-2012-2016-dr.Rdata',
                'current-results-2016-2020-dr.Rdata',
                'current-results-2012-2016-d1600.Rdata',
                'current-results-2012-2016-k100.Rdata',
                'current-results-2012-2016-k500.Rdata',
                'current-results-2012-2016-block.Rdata',
                'current-results-2012-2016-block-group.Rdata',
                
                'current-results-2016-2020-d1600.Rdata',
                'current-results-2016-2020-k100.Rdata',
                'current-results-2016-2020-k500.Rdata',
                'current-results-2016-2020-block.Rdata',
                'current-results-2016-2020-block-group.Rdata',
                
                'current-results-2012-2016-aspatial.Rdata',
                'current-results-2016-2020-aspatial.Rdata',
                
                'current-results-2012-2016-new-neighbors.Rdata',
                'current-results-2016-2020-new-neighbors.Rdata',
                
                'current-results-2012-2016-no-bg-controls.Rdata',
                'current-results-2016-2020-no-bg-controls.Rdata',
                
                'current-results-2016-2020-race-imps.Rdata'
                )
               )

results = rbindlist(lapply(files, FUN=function(file){
  
  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
    #print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }
    
    
    return(out)
    
  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
  l[grepl('2020',file),Year1:='2016']
  l[grepl('2020',file),Year2:='2020'] 
  l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
  l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
  l[,file:=file]
  
  return(l)
}))
 

### 2012-2016

t = data.table(Treatment = c('Main', 'Main',
                             'Pre-trend', 'Pre-trend',
                             'Aspatial', 'Aspatial',
                             'Dem. Ratio', 'Dem. Ratio',
                             'Include Same Household', 'Include Same Household',
                             '100 Neighbors', '100 Neighbors',
                             '500 Neighbors', '500 Neighbors',
                             'Mile Radius', 'Mile Radius',
                             'New Neighbors', 'New Neighbors',
                             'Census Block', 'Census Block',
                             'Census Block Group', 'Census Block Group',
                             'No BG Controls', 'No BG Controls'),
               
               Stat = rep(c('Coef','SE'),12),
               
               DED = c(
                results[Covariate=='DemSpExpDiff_nohh' & 
                                   file == 'results/current-results-2012-2016.Rdata' &
                                   Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                                   file == 'results/current-results-2012-2016.Rdata' &
                                   Model == 'ModelDemSpExpDems']$SE,
                 
                results[Covariate=='DemSpExpDiff_nohh' & 
                          file == 'results/current-results-2012-2016-pretrend.Rdata' &
                          Model == 'ModelDemSpExpDems']$Estimate,
                results[Covariate=='DemSpExpDiff_nohh' & 
                                  file == 'results/current-results-2012-2016-pretrend.Rdata' &
                                  Model == 'ModelDemSpExpDems']$SE,
                
                 results[Covariate=='DemExpDiff' & 
                                   file == 'results/current-results-2012-2016-aspatial.Rdata' &
                                   Model == 'ModelDemExpDems']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                                   file == 'results/current-results-2012-2016-aspatial.Rdata' &
                                   Model == 'ModelDemExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                                   file == 'results/current-results-2012-2016-dr.Rdata' &
                                   Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                                   file == 'results/current-results-2012-2016-dr.Rdata' &
                                   Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                                   file == 'results/current-results-2012-2016-k100.Rdata' &
                                   Model == 'ModelDemSpExpDems']$Estimate,
                results[Covariate=='DemSpExpDiff_nohh_k100' & 
                          file == 'results/current-results-2012-2016-k100.Rdata' &
                          Model == 'ModelDemSpExpDems']$SE,
                 
                results[Covariate=='DemSpExpDiff_nohh_k500' & 
                          file == 'results/current-results-2012-2016-k500.Rdata' &
                          Model == 'ModelDemSpExpDems']$Estimate,
                results[Covariate=='DemSpExpDiff_nohh_k500' & 
                          file == 'results/current-results-2012-2016-k500.Rdata' &
                          Model == 'ModelDemSpExpDems']$SE,
                 
                results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                          file == 'results/current-results-2012-2016-d1600.Rdata' &
                          Model == 'ModelDemSpExpDems']$Estimate,
                results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                          file == 'results/current-results-2012-2016-d1600.Rdata' &
                          Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                                   file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                                   Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                                   Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                                   file == 'results/current-results-2012-2016-block.Rdata' &
                                   Model == 'ModelDemBlockDems']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                                   Model == 'ModelDemBlockDems']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                                   Model == 'ModelDemBlockGroupDems']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                                   Model == 'ModelDemBlockGroupDems']$SE,
                
                results[Covariate=='DemSpExpDiff_nohh' & 
                          file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                          Model == 'ModelDemSpExpDems']$Estimate,
                results[Covariate=='DemSpExpDiff_nohh' & 
                          file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                          Model == 'ModelDemSpExpDems']$SE
                 
                 
                 ),
               
               DER = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemExpReps']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemBlockReps']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemBlockReps']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupReps']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE
                 
                 
               ),
               
               DEI = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemExpOths']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelDemExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemBlockOths']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelDemBlockOths']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupOths']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE
                 
                 
               ),
               
               RED = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpDems']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockDems']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockDems']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupDems']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE
                 
                 
               ),
               
               RER = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpReps']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockReps']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockReps']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupReps']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE
                 
                 
               ),
               
               REI = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpOths']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2012-2016-aspatial.Rdata' &
                           Model == 'ModelRepExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2012-2016-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2012-2016-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2012-2016-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2012-2016-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2012-2016-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockOths']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2012-2016-block.Rdata' &
                           Model == 'ModelRepBlockOths']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupOths']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2012-2016-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2012-2016-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE
                 
                 
               )
               
               
               
               
               
               
               
               
               
               )



t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]
t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]
t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]
t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]
t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]
t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]
t[Stat=='SE',Treatment:='']
t[,Stat:=NULL]


out = gt(t) %>%
  cols_label(
    `DED` = 'Democrats',
    `DER` = 'Republicans',
    `DEI` = 'Non-Partisans',
    `RED` = 'Democrats',
    `RER` = 'Republicans',
    `REI` = 'Non-Partisans'
  )%>%
  tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
  tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
  tab_spanner(label = 'Current effects: 2012-2016',columns = 2:7)%>%
  
  as_latex()%>%
  as.character()%>%
  str_replace_all('longtable','tabular')


write_file(out, 'tables/TabS12a.tex')  




### 2016-2020


t = data.table(Treatment = c('Main', 'Main',
                             'Pre-trend', 'Pre-trend',
                             'Aspatial', 'Aspatial',
                             'Dem. Ratio', 'Dem. Ratio',
                             'Include Same Household', 'Include Same Household',
                             '100 Neighbors', '100 Neighbors',
                             '500 Neighbors', '500 Neighbors',
                             'Mile Radius', 'Mile Radius',
                             'New Neighbors', 'New Neighbors',
                             'Census Block', 'Census Block',
                             'Census Block Group', 'Census Block Group',
                             'No BG Controls', 'No BG Controls',
                             'Race Posteriors','Race Posteriors'),
               
               Stat = rep(c('Coef','SE'),13),
               
               DED = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpDems']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockDems']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockDems']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupDems']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpDems']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpDems']$SE
                 
                 
               ),
               
               DER = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpReps']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockReps']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockReps']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupReps']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpReps']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpReps']$SE
                 
                 
               ),
               
               DEI = c(
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpOths']$Estimate,
                 results[Covariate=='DemExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelDemExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockOths']$Estimate,
                 results[Covariate=='DemBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelDemBlockOths']$SE,
                 
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupOths']$Estimate,
                 results[Covariate=='DemBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelDemBlockGroupOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE,
                 
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpOths']$Estimate,
                 results[Covariate=='DemSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelDemSpExpOths']$SE
                 
                 
               ),
               
               RED = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpDems']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpDems']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockDems']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockDems']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupDems']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpDems']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpDems']$SE
                 
                 
               ),
               
               RER = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpReps']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpReps']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockReps']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockReps']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupReps']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupReps']$SE,
                 
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpReps']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpReps']$SE
                 
                 
               ),
               
               REI = c(
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-pretrend.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpOths']$Estimate,
                 results[Covariate=='RepExpDiff' & 
                           file == 'results/current-results-2016-2020-aspatial.Rdata' &
                           Model == 'ModelRepExpOths']$SE,
                 
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='DRSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-dr.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff' & 
                           file == 'results/current-results-2016-2020-full-knn.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k100' & 
                           file == 'results/current-results-2016-2020-k100.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_k500' & 
                           file == 'results/current-results-2016-2020-k500.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh_d1600' & 
                           file == 'results/current-results-2016-2020-d1600.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepNewDiff' & 
                           file == 'results/current-results-2016-2020-new-neighbors.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockOths']$Estimate,
                 results[Covariate=='RepBlockDiff' & 
                           file == 'results/current-results-2016-2020-block.Rdata' &
                           Model == 'ModelRepBlockOths']$SE,
                 
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupOths']$Estimate,
                 results[Covariate=='RepBlockGroupDiff' & 
                           file == 'results/current-results-2016-2020-block-group.Rdata' &
                           Model == 'ModelRepBlockGroupOths']$SE,
                 
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-no-bg-controls.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE,
                 
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpOths']$Estimate,
                 results[Covariate=='RepSpExpDiff_nohh' & 
                           file == 'results/current-results-2016-2020-race-imps.Rdata' &
                           Model == 'ModelRepSpExpOths']$SE
                 
                 
               )
               
               
               
               
               
               
               
               
               
)



t[,DED:=ifelse(Stat=='Coef',round(DED,3),paste0('(',round(DED,3),')'))]
t[,DER:=ifelse(Stat=='Coef',round(DER,3),paste0('(',round(DER,3),')'))]
t[,DEI:=ifelse(Stat=='Coef',round(DEI,3),paste0('(',round(DEI,3),')'))]
t[,RED:=ifelse(Stat=='Coef',round(RED,3),paste0('(',round(RED,3),')'))]
t[,RER:=ifelse(Stat=='Coef',round(RER,3),paste0('(',round(RER,3),')'))]
t[,REI:=ifelse(Stat=='Coef',round(REI,3),paste0('(',round(REI,3),')'))]
t[Stat=='SE',Treatment:='']
t[,Stat:=NULL]


out = gt(t) %>%
  cols_label(
    `DED` = 'Democrats',
    `DER` = 'Republicans',
    `DEI` = 'Non-Partisans',
    `RED` = 'Democrats',
    `RER` = 'Republicans',
    `REI` = 'Non-Partisans'
  )%>%
  tab_spanner(label = 'DV: Democratic Registration',columns = 2:4)%>%
  tab_spanner(label = 'DV: Republican Registration',columns = 5:7)%>%
  tab_spanner(label = 'Current effects: 2016-2020',columns = 2:7)%>%
  
  as_latex()%>%
  as.character()%>%
  str_replace_all('longtable','tabular')

  
write_file(out, 'tables/TabS12b.tex')  






